/*
 *      Copyright 2012  Samsung Electronics Co., Ltd
 *
 *      Licensed under the Flora License, Version 1.0 (the "License");
 *      you may not use this file except in compliance with the License.
 *      You may obtain a copy of the License at
 *
 *              http://www.tizenopensource.org/license
 *
 *      Unless required by applicable law or agreed to in writing, software
 *      distributed under the License is distributed on an "AS IS" BASIS,
 *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *      See the License for the specific language governing permissions and
 *      limitations under the License.
 */

$(document).delegate("#appsvc", "pageinit", function() {
	var app;

	$("#appsvc .ui-btn-back").bind("vclick", function() {
		if (app) {
			try {
				var appsvcData = [new tizen.ApplicationServiceData("name", [app.name]),
									new tizen.ApplicationServiceData("version", [app.version])];
				appsvc.replyResult(appsvcData);
			} catch (exc) {
				alert("replyResult failed: " + exc.message);
				appsvc.replyFailure();
			}
		}
		tizen.application.exit();
		return false;
	});

	var appsvc = tizen.application.getAppService();

	if (appsvc) {
		var data = appsvc.data;

		for (var i = 0; i < data.length; i++) {
			if (data[i].key == "id") {
				break;
			}
		}
		if (i == data.length) {
			$("#msg").html("Invalid appservice request received");
			appsvc.replyFailure();
		} else {
			var appId = data[i].value[0];

			app = tizen.application.getAppInfo(appId);

			$("#msg").html("The following app information requested through appservice<br/>["
							+ appId
							+ "]<br/><br/>The following reply data will be sent when you exit this application<br/><br/>[Reply Data]<br/>name: "
							+ app.name
							+ " / version: "
							+ app.version);
		}
	} else {
		$("#msg").html("No Appservice request received");
	}
});
